package com.naiterui.ehp.bs.recommend.controller.inner;

import com.naiterui.ehp.bp.bo.recommend.param.RecomAuditParamBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.recommend.bean.RecomAuditLog;
import com.naiterui.ehp.bs.recommend.exception.ExceptionCodes;
import com.naiterui.ehp.bs.recommend.service.IPrescriptionService;
import com.naiterui.ehp.bs.recommend.service.IRecomAuditService;
import com.naiterui.ehp.bs.recommend.vo.prescription.PrescriptionDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @Description 药师内部接口
 * @Author gongbaoqiang
 * @Date 2019/12/7 5:08 PM
 */
@Slf4j
@Validated
@Api(tags = {SwaggerTag.PHARMACIST + SwaggerTag.RECOM, SwaggerTag.INNER_API + SwaggerTag.RECOM})
@RequestMapping("recommend/inner/pharmacist")
@RestController
public class InnerPharmacistController {

    @Autowired
    private IRecomAuditService recomAuditService;

    @Autowired
    private IPrescriptionService prescriptionService;

    /**
     * 处方审核
     *
     * @return
     *
     * @throws BusinessException
     */
    @ApiOperation(value = "处方审核")
    @PostMapping("audit")
    public ResponseVO<Void> audit(@RequestBody RecomAuditParamBO auditParamBO) throws BusinessException,
            UnsupportedEncodingException {
        log.info("药师处方审核：auditParamBO={}", auditParamBO.toString());

        if (RecomAuditLog.AUDIT_STATUS_REJECT == auditParamBO.getStatus() && StringUtils.isBlank(auditParamBO.getRemark())) {
            log.warn("审核不通过，原因必填 recomId:{}, status:{}", auditParamBO.getRecomId(), auditParamBO.getStatus());
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        String oper = StringUtils.isEmpty(auditParamBO.getOperator()) ? "" : URLDecoder.decode(auditParamBO.getOperator(), "UTF-8");
        recomAuditService.audit(auditParamBO, oper);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * <pre>
     *     获取处方笺详情
     * </pre>
     *
     * @param recommendId
     *
     * @return
     *
     * @throws BusinessException
     */
    @ApiOperation(value = "获取处方笺详情")
    @ApiImplicitParam(name = "recommendId", value = "处方ID", paramType = "query", dataType = "long", required = true)
    @PostMapping("prescription/detail")
    public ResponseVO<PrescriptionDetailVO> getPrescriptionDetail(Long recommendId)
            throws BusinessException {

        PrescriptionDetailVO prescription = prescriptionService.getPrescriptionDetailForDoctor(null, null, recommendId, false);
        if (null != prescription.getPharmacistAuditTime()) {
            prescription.setRecomTime(prescription.getPharmacistAuditTime());
        }
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, prescription);
    }
}
